//
//  FoodViewController.m
//  Kiemtragiuaky
//
//  Created by huy ngo on 4/21/15.
//  Copyright (c) 2015 Chau Nguyen DAB. All rights reserved.
//

#import "FoodViewController.h"
#import "FoodCell.h"
@interface FoodViewController ()
@property(nonatomic,strong)NSArray* foodsArray;
@end

@implementation FoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.foodTable.delegate= self;
    self.foodTable.dataSource = self;
    NSString* path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask,  YES)[0];
    NSString* databaseFileName = @"data.plist";
    NSString* databaseFilePath = [path stringByAppendingString:databaseFileName];
    self.foodsArray = [NSArray arrayWithContentsOfFile:databaseFilePath];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* identifier = @"foodcellid";
    FoodCell* cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        [tableView registerClass:[FoodCell class] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    NSDictionary* food = [self.foodsArray objectAtIndex:indexPath.row];
    // Configure the cell...
    cell.Name.text = [food objectForKey:@"name"];
    cell.descrip.text = [food objectForKey:@"descrip"];
    NSData* dataImage =[NSData dataWithContentsOfURL:[food objectForKey:@"url"]];
    cell.foodImage.image = [UIImage imageWithData:dataImage];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
